package com.dsdl.chatapp.module.chat.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dsdl.chatapp.R;
import com.dsdl.chatapp.contacts.Constant;
import com.dsdl.chatapp.data.SocketMessage;
import com.dsdl.chatapp.common.utils.SPUtil;
import com.dsdl.chatapp.common.utils.StringUtils;

import java.util.List;

/**
 * Created by zzg on 2016/8/13.
 * 聊天窗口适配器（RecyclerView）
 */
public class ChatWindowAdapter extends RecyclerView.Adapter<ChatWindowAdapter.ViewHolder>{

    private Context mContext;
    private List<SocketMessage> mList;
    private LayoutInflater mInflater;
    private long showedTimestamp = 0;//已经显示过时间的时间戳
    public ChatWindowAdapter(Context context, List<SocketMessage> list){
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_chat_window,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mList == null){
            return;
        }
        if (mList.size() <= position){
            return;
        }
        if (SPUtil.getSharedStringData(Constant.USER_PHONE,"").equals(mList.get(position).getFromUser())){
            holder.llSend.setVisibility(View.VISIBLE);
            holder.llReceived.setVisibility(View.GONE);
            holder.tvSendMsg.setText(mList.get(position).getContent());
        }else {
            holder.llSend.setVisibility(View.GONE);
            holder.llReceived.setVisibility(View.VISIBLE);
            holder.tvReceivedMsg.setText(mList.get(position).getContent());
        }

        if (mList.get(position).isShowTime()){
            long currentPositionTimestamp = mList.get(position).getCreateTime();
            String time = StringUtils.timestamp2DateTime(currentPositionTimestamp);
            holder.tvTime.setText(time);
            holder.tvTime.setVisibility(View.VISIBLE);
        }else {
            if (position >0){
                long lastPositionTimestamp = mList.get(position-1).getCreateTime();
                long currentPositionTimestamp = mList.get(position).getCreateTime();
                if ((currentPositionTimestamp - lastPositionTimestamp) >= 3*60*1000){
                    String time = StringUtils.timestamp2DateTime(currentPositionTimestamp);
                    holder.tvTime.setText(time);
                    holder.tvTime.setVisibility(View.VISIBLE);
                    showedTimestamp  = currentPositionTimestamp;
                    mList.get(position).setShowTime(true);
                }else {
                    if ((currentPositionTimestamp - showedTimestamp) >= 3*60*1000){
                        String time = StringUtils.timestamp2DateTime(currentPositionTimestamp);
                        holder.tvTime.setText(time);
                        holder.tvTime.setVisibility(View.VISIBLE);
                        showedTimestamp  = currentPositionTimestamp;
                        mList.get(position).setShowTime(true);
                    }
                }

            }else {
                long thisTimestamp = mList.get(position).getCreateTime();
                String time = StringUtils.timestamp2DateTime(thisTimestamp);
                holder.tvTime.setText(time);
                holder.tvTime.setVisibility(View.VISIBLE);
                showedTimestamp = thisTimestamp;
                mList.get(position).setShowTime(true);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout llReceived;
        ImageView ivReceiverPhoto;
        TextView tvReceivedMsg;
        LinearLayout llSend;
        TextView tvSendMsg;
        ImageView ivSenderPhoto;
        TextView tvTime;
        public ViewHolder(View itemView){
            super(itemView);
            llReceived = (LinearLayout) itemView.findViewById(R.id.ll_item_received);
            ivReceiverPhoto = (ImageView) itemView.findViewById(R.id.iv_item_receiver_photo);
            tvReceivedMsg = (TextView) itemView.findViewById(R.id.tv_item_received_msg);
            llSend = (LinearLayout) itemView.findViewById(R.id.ll_item_send);
            tvSendMsg = (TextView) itemView.findViewById(R.id.tv_item_send_msg);
            ivSenderPhoto = (ImageView) itemView.findViewById(R.id.iv_item_sender_photo);
            tvTime = (TextView) itemView.findViewById(R.id.tv_item_time);
        }
    }
}
